/*
 *
 * *******************************************************************
 *   @项目名称: BHex Android
 *   @文件名称: UserInfo.java
 *   @Date: 11/29/18 3:21 PM
 *   @Author: chenjun
 *   @Copyright（C）: 2018 BlueHelix Inc.   All rights reserved.
 *   注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的.
 *  *******************************************************************
 *
 */

package io.bhex.sdk.account;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import java.io.Serializable;

import io.bhex.sdk.account.bean.UserInfoBean;
import io.bhex.sdk.BhexSdk;
import io.bhex.baselib.constant.AppData;
import io.bhex.sdk.data_manager.NetWorkApiManager;
import io.bhex.baselib.core.SPEx;
import io.bhex.baselib.network.Utils.Convert;
import io.bhex.baselib.network.Utils.CookieUtils;
import io.bhex.baselib.utils.DebugLog;
import io.bhex.baselib.utils.ToastUtils;


public class UserInfo {
    //保存用户数据


    public static abstract class AfterLoginInterFace implements Serializable {
        public abstract void AfterLogin(boolean bSuccess);
    }

    public static void saveUserInfo(UserInfoBean data){

        SPEx.set(AppData.SPKEY.USER_INFO_KEY, Convert.toJson(data));
        SPEx.set(AppData.SPKEY.USER_INFO_ID, data.getUserId());
        SPEx.set(AppData.SPKEY.USER_INFO_EMAIL, data.getEmail());
        SPEx.set(AppData.SPKEY.USER_INFO_MOBILE, data.getMobile());
        SPEx.set(AppData.SPKEY.USER_INFO_BINDGA, data.isBindGA());
        SPEx.set(AppData.SPKEY.USER_INFO_ACCOUNT_ID, data.getDefaultAccountId());
    }

    /**
     * 获取用户信息
     * @return
     */
    public static UserInfoBean getUserInfo(){
       String userStr = SPEx.get(AppData.SPKEY.USER_INFO_KEY,"");
        if (!TextUtils.isEmpty(userStr)){
            UserInfoBean userInfoBean = Convert.fromJson(userStr, UserInfoBean.class);
            return userInfoBean;
        }
        return null;
    }

    //清除用户信息
    public static void clearUserInfo(){
//        CookieUtils.clearCookies(BhexSdk.getContext());
        SPEx.remove(AppData.SPKEY.USER_INFO_KEY);
        SPEx.remove(AppData.SPKEY.USER_INFO_ID);
        SPEx.remove(AppData.SPKEY.USER_INFO_EMAIL);
        SPEx.remove(AppData.SPKEY.USER_INFO_MOBILE);
        SPEx.remove(AppData.SPKEY.USER_INFO_BINDGA);
        SPEx.remove(AppData.SPKEY.USER_INFO_ACCOUNT_ID);
//        SPEx.remove(AppData.SPKEY.FINGER_PWD_KEY);
//        SPEx.remove(AppData.SPKEY.GESTURE_PWD_KEY);
        NetWorkApiManager.releaseTradeInstance();
    }

    /**
     * 获取默认的AccountId
     */
    public static String getUserId(){
        return SPEx.get(AppData.SPKEY.USER_INFO_ID, "");
    }

    /**
     * 获取默认的AccountId
     */
    public static String getDefaultAccountId(){
        return SPEx.get(AppData.SPKEY.USER_INFO_ACCOUNT_ID, "");
    }

    /**
     * 是否登录
     * @return
     */
    public static boolean isLogin(){
        boolean fingerOpen = SPEx.isFingerOpen();
        boolean fingerAuth = SPEx.isFingerAuth();
        String cookieToken = CookieUtils.getCookieToken();
        if (TextUtils.isEmpty(cookieToken)) {
            UserInfo.clearUserInfo();
            DebugLog.i("==清除了用户信息==");
            return false;
        }
        if (fingerOpen) {
            return !TextUtils.isEmpty(SPEx.get(AppData.SPKEY.USER_INFO_ID, ""))&&(fingerAuth);
        }else{
            return !TextUtils.isEmpty(SPEx.get(AppData.SPKEY.USER_INFO_ID, ""));
        }
    }

    /**
     * 是否登录 未登录，自动跳转登录
     * @return
     */
    public static boolean isLogin(Context context,String caller){
        boolean fingerOpen = SPEx.isFingerOpen();
        boolean fingerAuth = SPEx.isFingerAuth();
        String cookieToken = CookieUtils.getCookieToken();
        if (TextUtils.isEmpty(cookieToken)) {
            UserInfo.clearUserInfo();
            DebugLog.i("==清除了用户信息==");
            BhexSdk.NoCookieNotity(context,"");
            return false;
        }

        if (fingerOpen) {
            if(!TextUtils.isEmpty(SPEx.get(AppData.SPKEY.USER_INFO_ID, ""))&&(fingerAuth)){
                return true;
            }
            BhexSdk.NoCookieNotity(context,"");
            return false;
        }else{
            if (TextUtils.isEmpty(SPEx.get(AppData.SPKEY.USER_INFO_ID, ""))) {
                //IntentUtils.goLogin(context,caller);
                BhexSdk.NoCookieNotity(context,"");
                return false;
            }
            return true;
        }
    }

    /**
     * 是否登录 未登录，自动跳转登录
     * @return
     */
    public static boolean LoginAndGoin(Activity context, String caller, int requestCode){
        boolean fingerOpen = SPEx.isFingerOpen();
        boolean fingerAuth = SPEx.isFingerAuth();
        String cookieToken = CookieUtils.getCookieToken();
        if (TextUtils.isEmpty(cookieToken)) {
            UserInfo.clearUserInfo();
            DebugLog.i("==清除了用户信息==");
            BhexSdk.NoCookieNotity(context,caller, requestCode);
            return false;
        }

        if (fingerOpen) {
            if(!TextUtils.isEmpty(SPEx.get(AppData.SPKEY.USER_INFO_ID, ""))&&(fingerAuth)){
                return true;
            }
            BhexSdk.NoCookieNotity(context,caller, requestCode);
            return false;
        }else{
            if (TextUtils.isEmpty(SPEx.get(AppData.SPKEY.USER_INFO_ID, ""))) {
                //IntentUtils.goLogin(context,caller);
                BhexSdk.NoCookieNotity(context,caller, requestCode);
                return false;
            }
            return true;
        }
    }
}
